package spotGUI.floatingChatGUI;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.net.URL;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Properties;

import javax.swing.JPanel;

import spotGUI.utils.EmptyContentAreaButton;
import utils.StackTracePrinter;

public class EmoticonsChooser extends JPanel {
	
	private Properties shortcuts = new Properties();
	
	
	//folder of the emoticon pack, relative to images/Emoticons/
	public EmoticonsChooser(LinkedList<String> shortcutsList, FloatingChat frame) {
		
		try {
			shortcuts.load(new FileReader(ShortcutsParser.FILE_PATH));
		} catch (Exception e) {
			StackTracePrinter.print(e);
		}
		
		setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));

		Iterator<String> it = shortcutsList.iterator();
		HashSet<URL> alreadyAddedIcons = new HashSet<URL>();
		while (it.hasNext()) {
			String temp = it.next();
			URL url = EmoticonsChooser.class.getResource("/images/Emoticons/"+shortcuts.getProperty(temp));
			if (!alreadyAddedIcons.contains(url))
					add(new EmoticonButton(temp, url, frame));
			alreadyAddedIcons.add(url);
			
		}
		setVisible(true);
	}
}

class EmoticonButton extends EmptyContentAreaButton {
	
	public EmoticonButton(final String shortcut, URL icon, final FloatingChat frame) {
		
		super(icon, null);
		
		addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				frame.insertString(shortcut);
			}
		});
		setPreferredSize(new Dimension (32, 32));
		setBorderPainted(false);
		setOpaque(false);
	}
	
	
}
